home *** CD-ROM | disk | FTP | other *** search
/ Aminet 19 / Aminet 19 (1997)(GTI - Schatztruhe)[!][Jun 1997].iso / Aminet / comm / net / AMarquee1_43.lha / AMarquee / examples / SyncTest.c < prev   
C/C++ Source or Header  |  1997-04-28  |  3KB  |  109 lines

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. #include <dos/dos.h>
  7. #include <clib/dos_protos.h>
  8. #include <clib/exec_protos.h>
  9.  
  10. #include <clib/AMarquee_protos.h>
  11.  
  12. #include <pragmas/AMarquee_pragmas.h>
  13.  
  14. #define UNLESS(x) if (!(x))
  15.  
  16. struct Library * AMarqueeBase = NULL;
  17. struct QSession * session     = NULL;
  18.  
  19. void CleanExit(void)
  20. {
  21.   if (session)      QFreeSession(session);       /* This MUST be done before we close the library! */
  22.   if (AMarqueeBase) CloseLibrary(AMarqueeBase);
  23.   printf("All done.\n");
  24. }
  25.  
  26. /* Main program--simply increments our counter whenever someone else increments theirs */
  27. int main(int argc, char ** argv)
  28. {
  29.   char * connectTo, * progName;
  30.   int port = 2957;
  31.   int count = 0, syncOp;
  32.   
  33.   atexit(CleanExit);
  34.     
  35.   printf("SyncTest:  This program continually updates an integer on the server.\n");
  36.   printf("           It uses QGOF_SYNC synchronization so that any other clients\n");
  37.   printf("           who subscribe to this entry (named \"count\") should never\n");
  38.   printf("           miss an update.\n\n");
  39.   
  40.   printf("Usage Note:  SyncTest [hostname=localhost] [name=synctest]\n");
  41.   connectTo = (argc > 1) ? argv[1] : "localhost";
  42.   progName  = (argc > 2) ? argv[2] : "synctest";
  43.  
  44.   if ((AMarqueeBase = OpenLibrary("amarquee.library",37L)) == NULL)
  45.   {
  46.     printf("Couldn't open amarquee.library v37!\n");
  47.     exit(RETURN_ERROR);
  48.   }
  49.   printf("Connecting to %s:%i...\n",connectTo, port);
  50.   if ((session = QNewSession(connectTo, port, progName)) == NULL)
  51.   {
  52.     printf("Couldn't connect to server %s:%i\n",connectTo, port);
  53.     CloseLibrary(AMarqueeBase);
  54.     exit(RETURN_WARN);
  55.   }
  56.   printf("SyncTest connected to server %s:%i\n",connectTo, port);
  57.  
  58.   /* Setup */
  59.   UNLESS(QSetOp(session, "count", &count, sizeof(count))) 
  60.   {
  61.     printf("Couldn't do QSetOp(), aborting!\n");
  62.     Signal(FindTask(NULL), SIGBREAKF_CTRL_C);
  63.   }
  64.   syncOp = QGo(session,QGOF_SYNC);  /* Start it off! */
  65.  
  66.   while(1)
  67.   {
  68.     struct QMessage * qMsg;
  69.     ULONG signals = (1L << session->qMsgPort->mp_SigBit) | (SIGBREAKF_CTRL_C);
  70.  
  71.     /* Wait for next message from the server */
  72.     signals = Wait(signals);
  73.     
  74.     if (signals & (1L << session->qMsgPort->mp_SigBit))
  75.     {
  76.       while(qMsg = (struct QMessage *) GetMsg(session->qMsgPort))
  77.       {
  78.         if (qMsg->qm_Status != QERROR_NO_ERROR) 
  79.         {
  80.           printf("Error %i detected! Aborting\n", qMsg->qm_Status);
  81.           Signal(FindTask(NULL), SIGBREAKF_CTRL_C);
  82.         }
  83.         else
  84.         {
  85.           if (qMsg->qm_ID == syncOp)
  86.           {
  87.             /* It's the sync signal, meaning everyone else has seen our update */
  88.             count++;
  89.  
  90.             printf("Synced, now uploading %li... ",count); fflush(stdout);
  91.  
  92.             UNLESS((QSetOp(session, "count", &count, sizeof(count)))&&
  93.                    (syncOp = QGo(session, QGOF_SYNC)))
  94.             {
  95.               printf("Upload failed, aborting\n");
  96.               Signal(FindTask(NULL), SIGBREAKF_CTRL_C);
  97.             }
  98.             printf("Now awaiting sync signal (id %i).\n",syncOp);
  99.           }
  100.         }
  101.         FreeQMessage(session,qMsg);
  102.       }
  103.     }
  104.     if (signals & SIGBREAKF_CTRL_C) break;  /* Quit if CTRL-C pressed */
  105.   }
  106.   
  107.   /* CleanExit called here via the atexit() feature */
  108. }
  109.